package cn.mothin.frame.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component("51d29d4cbe310e3bec0b5c1e")
public class SpringUtil implements ApplicationContextAware {
	private static ApplicationContext applicationContext;

	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	@Override
	public void setApplicationContext(ApplicationContext arg0)
			throws BeansException {
		applicationContext = arg0;
	}
	
	public static Object getJavascriptBean(String ns){
		List<IJavascriptService> list = getClassListWithInterface(IJavascriptService.class);
		for (IJavascriptService service : list) {
			Class<?> c = service.getClass();
			String className=c.getName();
			if(className.contains("$$")){
				String[] ss=className.split("\\$\\$");
				String cn=ss[0];
				if(cn.equals(ns)){
					return service;
				}
			}
		}
		return null;
	}

	public static <T> List<T> getClassListWithInterface(Class<T> findInterface) {
		Map<String, T> map = applicationContext.getBeansOfType(findInterface);
		return new ArrayList<T>(map.values());
	}
}
